package com.itic.appbase.applications.exports.ExportExcelWithZip;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileLock;

import com.itic.appbase.framework.utils.PropertyGetter;
import com.itic.appbase.framework.utils.StringHelper;

/**
 * @author crespo
 *
 */
public class FileUtils {
	private static final int BUFFER_SIZE = 50 * 1024;

	public static void copy(File src, File dst) {
		InputStream in = null;
		OutputStream out = null;
		try {
			in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
			out = new BufferedOutputStream(new FileOutputStream(dst), BUFFER_SIZE);
			byte[] buffer = new byte[BUFFER_SIZE];
			int len = 0;
			while ((len = in.read(buffer)) > 0) {
				out.write(buffer, 0, len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != in) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != out) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void releaseFileLockQuietly(FileLock lock) {
		if (lock == null || !lock.isValid()) {
			return;
		}
		try {
			lock.release();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 文件内容比较，通过比较文件的md5摘要来判断文件内容是否一致
	 * 
	 * @param src
	 *            源文件
	 * @param dest
	 *            目标文件
	 * @return 文件内容一样返回true,否则false
	 */
	public static boolean fileCompare(File src, File dest) {
		if (src == null || dest == null) {
			throw new IllegalArgumentException("source file or dest file must be not null");
		}
		String srcFileMD5 = MD5Util.fileMd5(src);
		String destFileMD5 = MD5Util.fileMd5(dest);

		return srcFileMD5 == null ? destFileMD5 == null : srcFileMD5.equals(destFileMD5);
	}

	public static String toString(File file, String encoding) {
		try {
			if (StringHelper.isBlank(encoding)) {
				return org.apache.commons.io.FileUtils.readFileToString(file);
			}
			return org.apache.commons.io.FileUtils.readFileToString(file, encoding);
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}

	public static String toString(File file) {
		return toString(file, null);
	}
	
	/**
	 * 遍历某个文件目录下是否存在某个文件
	 * 
	 * @param dir
	 * 			  	目录
	 * @param fileName
	 * 				 文件名
	 * @return		若存在给定的文件返回true，否则返回false
	 * @throws IOException
	 */
	public static Boolean isExistFile(String dir,String fileName) throws IOException {
		File file = new File(dir);
		File[] files = file.listFiles();
		if(files != null) {
			for(File f : files) {
				if(f.getName().equals(fileName)) {
					return true;
				}
			}
			return false;
		}
		return false;
	}
	
	public static Boolean isExistFile1(String dir,String fileName) throws IOException {
		File file = new File(dir);
		File[] files = file.listFiles();
		if(files != null) {
			for(File f : files) {
				if(f.isDirectory()) {
					File[] newFiles = f.listFiles();
					if(newFiles != null) {
						for(File ff : newFiles) {
							if(ff.getName().equals(fileName)) {
								return true;
							}
						}
					}
				} else if(f.isFile()) {
					if(f.getName().equals(fileName)) {
						return true;
					}
				}
			}
			return false;
		}
		return false;
	}
	
	/**
	 * 
	 * 获取当前计算机用户的根目录，并且创建"fileupload"文件夹
	 */
	public static String getRootPath() {
		// 获取用户目录
        String absolutePath = System.getProperties().getProperty("user.home");
        String dirName = PropertyGetter.getString("app.code");
        String rootDir = "/" + dirName + "_fileupload";
        //创建fileupload文件夹
        File root = new File(absolutePath + rootDir);
        if(!root.exists()) {
			root.mkdir();
		}
        return absolutePath + rootDir + "/";
	}
	
	public static void copyDir(File srcDir, File dstDir, boolean override) {

		if (srcDir == null || dstDir == null || !srcDir.exists() ) {
			return;
		}
		if (!dstDir.exists()) {
			dstDir.mkdirs();
		}

		File[] files = srcDir.listFiles();

		for (File file : files) {
			File dstFile = new File(dstDir.getAbsolutePath() + "/"
					+ file.getName());
			if (file.isFile()) {
				FileUtils.copy(file, dstFile);
			}
			if (file.isDirectory()) {
				copyDir(file, dstFile, false);
			}
		}

	}
}
